import Vue from 'vue'
import vuex from 'vuex'

Vue.use(vuex)

export default new vuex.Store({
    state:{
        todoList: localStorage.getItem('todoLists') ? JSON.parse(localStorage.getItem('todoLists')) : []
    },
    mutations: {
        deleItem(state, data) {
            state.todoList.splice(function(data){
                return state.todoList.findIndex(item => item.id === data)
            }, 1)
            localStorage.setItem('todoLists', JSON.stringify(state.todoList))
        },
        addItem(state, data) {
            state.todoList.unshift(data)
            localStorage.setItem('todoLists', JSON.stringify(state.todoList))
        },
        changeStatus(state, data) {
            state.todoList[state.todoList.findIndex(item => item.id === data)].finished = !state.todoList[state.todoList.findIndex(item => item.id === data)].finished
            localStorage.setItem('todoLists', JSON.stringify(state.todoList))
        }
    }
})